//
//  SACategoryView.m
//  saasRetail
//
//  Created by ice on 2025/3/27.
//

#import "SACategoryView.h"
#import "SACategoryCell.h"
#import "SAServiceMarketModel.h"

@interface SACategoryView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation SACategoryView

-(instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        [self initUI];
    }
    return self;
}

- (UIView *)listView { 
    return self;
}

#pragma mark - UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.children.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"SACategoryCell";
    SACategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.model = self.model.children[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SAServiceMarketModel *model = self.model.children[indexPath.row];
    !self.marketBlock ?: self.marketBlock(model);
}

#pragma mark - initUI
-(void)initUI {
    [self addSubview:self.collectionView];
}

-(UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flow.itemSize = CGSizeMake(kScaleNum(152), kScaleNum(90));
        flow.minimumInteritemSpacing = kScaleNum(1);
        //    //设置每行之间的最小间隔
        flow.minimumLineSpacing = kScaleNum(1);
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScaleNum(90)) collectionViewLayout:flow];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[SACategoryCell class] forCellWithReuseIdentifier:@"SACategoryCell"];
    }
    return _collectionView;
}


@end
